home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / recio202.zip / testchp.c < prev    next >
C/C++ Source or Header  |  1994-05-05  |  7KB  |  245 lines

  1. /* testchp.c - tests character delimited put functions */
  2. /* recio version 2.02, release May 5, 1994 */
  3. /* Copyright (C) 1994 William Pierpoint */
  4.  
  5. #include <ctype.h>
  6. #include <errno.h>
  7. #include <limits.h>
  8. #include <float.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <io.h>
  13.  
  14. #include "recio.h"
  15.  
  16. /* errors to stderr */
  17. #define errout  stdout
  18.  
  19. /****************************************************************************
  20. Dynamic string concat function, kludged for use with recio.  This function 
  21. is not part of recio nor was rseterr() designed to work with it, but you get 
  22. the idea. 
  23. Precondition: set dst to null ptr when declaring it, e.g. char *dst=NULL;
  24. Syntax: the assignment operator is mandatory, i.e. dst=scats(dst, src);
  25. Obligation: free dst when finished with it.
  26. *****************************************************************************/
  27. char *                  /* return dst                                       */
  28.     scats(              /* concat string dynamically                        */
  29.         char *dst,      /* destination string pointer                       */
  30.         char *src)      /* source string pointer                            */
  31. /****************************************************************************/
  32. {                       
  33.   size_t dlen;          /* strlen of dst */
  34.   size_t slen;          /* strlen of src */
  35.  
  36.   /* if null src pointer or src empty, do nothing */
  37.   if (!src || !*src) {
  38.     goto done;
  39.   }
  40.   
  41.   if (dst) {
  42.     dlen = strlen(dst);
  43.     slen = strlen(src);
  44.     do {
  45.       dst = (char *) realloc(dst, dlen+slen+1);
  46.       if (!dst) {
  47.         if (rseterr(NULL, ENOMEM)) goto done;
  48.       }
  49.     } while (!dst);
  50.     strcat(dst, src);
  51.   } else {
  52.      do {
  53.        dst = strdup(src);
  54.        if (!dst) {
  55.          if (rseterr(NULL, ENOMEM)) goto done;
  56.        }
  57.      } while (!dst);
  58.    }
  59. done:
  60.   return dst;
  61. }
  62.  
  63. enum {RESET, INCR, REPORT};
  64. /****************************************************************************/
  65. void                         /* return nothing                              */
  66.     warnttl(                 /* warning totals                              */
  67.         int action,          /* action (0=reset, 1=increment, 2=report)     */
  68.         int warnum)          /* warning number                              */
  69. /****************************************************************************/
  70. {
  71.   static unsigned int widthcnt=0;
  72.   static unsigned int noregcnt=0;
  73.  
  74.   switch (action) {
  75.   case RESET:
  76.     widthcnt=0;
  77.     return;
  78.   case INCR:
  79.     switch(warnum) {
  80.     case R_WWIDTH: 
  81.       widthcnt++; 
  82.       return;
  83.     case R_WNOREG: 
  84.       noregcnt++; 
  85.       return;
  86.     }
  87.     return;
  88.   case REPORT:
  89.     fprintf(errout, "\n");
  90.     if (widthcnt) 
  91.       fprintf(errout, "WARNING -- %s -- %u times\n", 
  92.        rstrwarning(R_WWIDTH), widthcnt);
  93.     if (noregcnt) 
  94.       fprintf(errout, "WARNING -- %s\n", rstrwarning(R_WNOREG));
  95.     return;
  96.   }
  97. }
  98.  
  99. /****************************************************************************/
  100. void                         /* returns nothing                             */
  101.     rwarnfn(                 /* recio callback warning function             */
  102.         REC *rp)             /* record pointer                              */
  103. /****************************************************************************/
  104. {
  105.   if (risvalid(rp)) warnttl(INCR, rwarning(rp));
  106. }
  107.  
  108. /****************************************************************************/
  109. void                         /* returns nothing                             */
  110.     rerrfn(                  /* recio callback error function               */
  111.         REC *rp)             /* record pointer                              */
  112. /****************************************************************************/
  113. {
  114.   if (risvalid(rp)) {
  115.   
  116.     /* determine cause of error */
  117.     switch (rerror(rp)) {
  118.  
  119.     case R_ENOPUT:   /* could not write data */
  120.       fprintf(errout, "ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
  121.       break;
  122.  
  123.     /* fatal errors (R_EINVMOD, R_EINVAL, R_ENOMEM) */
  124.     default:
  125.       fprintf(errout, "FATAL ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
  126.       abort();
  127.       break;
  128.     }
  129.   
  130.   /* invalid record pointer */
  131.   } else {
  132.     switch (errno) {
  133.  
  134.     /* non-fatal errors */
  135.     case EACCES:
  136.     case EMFILE:
  137.       fprintf(errout, "WARNING: %s\n", strerror(errno));
  138.       break;
  139.  
  140.     /* fatal errors (EINVAL, ENOMEM) */
  141.     default:
  142.       fprintf(errout, "FATAL ERROR: %s\n", strerror(errno));
  143.       abort();
  144.       break;
  145.     }
  146.   }
  147. }
  148.  
  149. /****************************************************************************/
  150. void putcolnumbers(void)
  151. /****************************************************************************/
  152. {
  153. puts("");
  154. puts("         1         2         3         4         5         6");
  155. puts("123456789012345678901234567890123456789012345678901234567890");
  156. }
  157.  
  158. /****************************************************************************
  159. main
  160. *****************************************************************************/
  161. int main()
  162. {
  163.   int j, k;                     /* loop indices */
  164.   int i;                        /* integer field */
  165.   unsigned int ui;              /* unsigned integer field */
  166.   long l;                       /* long field */
  167.   unsigned long ul;             /* unsigned long field */
  168.   float f;                      /* float field */
  169.   double d;                     /* double field */
  170.   int ch;                       /* character field */
  171.   char *str = NULL;             /* string field */
  172.   char str1[]="A";              /* string consisting of one letter */
  173.   
  174.   /* install warning and error functions */
  175.   rsetwarnfn(rwarnfn);
  176.   rseterrfn(rerrfn);
  177.   
  178.   /* if output not redirected */
  179.   if (isatty(fileno(stdout))) {
  180.     /* print instructions */
  181.     puts("TESTCHP version 2.02 Copyright (C) 1994 William Pierpoint");
  182.     puts("Tests recio character delimited put functions.");
  183.   }
  184.  
  185.   for (k=0; k<=1; k++) {
  186.     switch (k) {
  187.     case 1:
  188.       rsetfldch(recout, ',');
  189.       rsettxtch(recout, '"');
  190.       break;
  191.     }
  192.  
  193.     /* initialize data */
  194.     i = -1;
  195.     ui = 1;
  196.     l = -1L;
  197.     ul = 1L;
  198.     f = 1.111111;
  199.     d = 1.111111111111111111111;
  200.     ch = 'a';
  201.     if (str) *str='\0';
  202.     *str1 = toupper(ch);
  203.     str = scats(str, str1);
  204.  
  205.     /* loop through data */
  206.     for (j=0; j<11; j++) {
  207.  
  208.       rputi(recout,  i);
  209.       rputui(recout, ui);
  210.       rputl(recout,  l);
  211.       rputul(recout, ul);
  212.       rputf(recout,  f);
  213.       rputd(recout,  d);
  214.       rputc(recout,  ch);
  215.       rputs(recout,  str);
  216.  
  217.       i  *= 10;
  218.       ui *= 10;
  219.       l  *= 10L;
  220.       ul *= 10L;
  221.       f  *= 10.;
  222.       d  *= 10.;
  223.       ch += 1;
  224.       *str1 = toupper(ch);
  225.       str = scats(str, str1);
  226.  
  227.       rputrec(recout);
  228.     }
  229.   }
  230.   
  231.   /* free string fields */
  232.   free (str);
  233.   
  234.   /* check stream for warnings */
  235.   if (rwarning(recout)) warnttl(REPORT, NULL);
  236.  
  237.   /* check stream for error */
  238.   if (rerror(recout)) { 
  239.     fprintf(stdout, "\nERROR %s -- %s\n", 
  240.      rnames(recout), rerrstr(recout));
  241.     exit (EXIT_FAILURE);
  242.   }
  243.   return EXIT_SUCCESS;
  244. }
  245.